<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$errors = array();

$candidate_id = UV::readInt(strip_tags($route->getAdminId()));
$candidate = $db->getRow("select * from `candidates` where `candidate_id` = '$candidate_id' limit 1");
if (!$candidate)
	goToPage("/admin/candidates/view");

if (UV::readMQ($route->POST("act")) == 'post')
{
	$preview = mb_substr(UV::readMQ(strip_tags($route->POST("preview"))),0,700,"utf-8");
	$preview = str_replace("\n","<br />",$preview);
	$text = UV::readMQ(strip_tags($route->POST("text")));
	$text = str_replace("\n","<br />",$text);
	
	$query = "update `candidates` set `preview`='$preview',`text`='$text' where `candidate_id` = '$candidate_id' limit 1";
	if (!$db->sendQuery($query))
		$errors[] = "Ошибка редактирования";
	
	if (count($errors) == 0)
		goToPage("/admin/candidates/view");
}

require_once ADMIN.DS."head.php";
?>
<script>
$(document).ready(function(){
	$("#preview").keydown(function(e){
		if (e.keyCode != 8 && e.keyCode != 9 && e.keyCode != 46 && (e.keyCode < 37 || e.keyCode > 40))
			if ($(this).val().length > 700)
				return false;
	})
})
</script>
<div class="full">
	<div class="submenu_container">
		<a class="submenu_button" href='/admin/report/look'>Просмотр отчетов</a>
		<a class="submenu_button" href='/admin/report/create'>Создать отчет</a>
		<a class="submenu_button" href='/admin/polls/view'>Просмотр опросов</a>
		<a class="submenu_button" href='/admin/polls/create'>Создать опрос</a>
		<a class="submenu_button" href='/admin/candidates/view'>Кандидаты</a>
		<a class="submenu_button submenu_button_active">Ред. кандидата</a>
	</div>
	<?php
	if (count($errors) > 0)
		echo "<p style='color:red'>".implode("<br />",$errors)."</p>";
	?>
	<h3>Редактирование кандидата</h3>
	<div id="contentaddnews">
		<form method='post' enctype="multipart/form-data">
			<input type="hidden" name="act" value='post' />
			<div class="left label clear"><h3 style="margin-top:0"><?php echo $candidate['name']; ?></h3></div><br /><br />
			<div class="left label clear">Кратко (не более 700 символов)</div>
			<div class="clear"><textarea id='preview' style="width:100%" rows='5' name='preview'><?php echo str_replace("<br />","\n",$candidate['preview']); ?></textarea></div><br />
			<div class="left label clear">Полностью</div>
			<div class="clear"><textarea id='text' style="width:100%" rows='10' name='text'><?php echo str_replace("<br />","\n",$candidate['text'])?></textarea></div><br />
			<input type="submit" value="Редактировать" style="float:right" />
		</form>
	</div>
</div>

<?php require_once ADMIN.DS."footer.php"; ?>